/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EXtremecode.Utils;
using EXtremecode.PresentationLayer;
using EXtremecode.Logging;

public partial class ASCXDatePicker : System.Web.UI.UserControl,IASCXControl
{

    private bool isSetDisplayMethodCalled = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterClientScriptInclude("calendar", ResolveClientUrl("~/calendar/calendar.js"));
        Page.ClientScript.RegisterClientScriptInclude("calendarlang", ResolveClientUrl("~/calendar/lang/calendar-en.js"));
        Page.ClientScript.RegisterClientScriptInclude("calendarsetup", ResolveClientUrl("~/calendar/calendar-setup_ew.js"));
       
    }

    public override string ClientID
    {
        get
        {
            return CurrentClientId;
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        vldrRequired.ErrorMessage = string.Format("{0} : {1}",
            caption, ResourceProvider.GetGeneralResourceString("Message_FieldRequired"));

        vldrValidateDate.ErrorMessage = string.Format("{0} : {1}",
            caption, ResourceProvider.GetGeneralResourceString("Message_InvalidDate"));



        //Script block
        string currentClientId = this.CurrentClientId;
        string originalClientId = this.OriginalClientId;
        string inputClientId = this.InputClientId;
        string displayClientId = this.DisplayClientId;

        StringBuilder sbScriptBlock = new StringBuilder();

        sbScriptBlock.AppendLine("<script>");
        sbScriptBlock.AppendLine(
            string.Format("var {0} = document.getElementById('{0}');", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.OriginalCtrl = document.getElementById('{1}');",
                currentClientId, originalClientId));

        if (!IsReadOnly)
        {
            sbScriptBlock.AppendLine(
                string.Format("var {0} = document.getElementById('{0}');", inputClientId));

            sbScriptBlock.AppendLine(
               string.Format("{0}.CurrentCtrl = document.getElementById('{1}');",
                   inputClientId, currentClientId));
        }


        sbScriptBlock.AppendLine(
            string.Format("{0}.InputCtrl = document.getElementById('{1}');",
            currentClientId, inputClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.DisplayCtrl = document.getElementById('{1}');",
            currentClientId, displayClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.FormattingObject = new EmptyClass();", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.SetValue = SetTextValue;", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.GetValue = GetTextValue;", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.ResetValue = resetValue;", currentClientId));


        sbScriptBlock.AppendLine(
            string.Format("{0}.SetDateValue = setDateValue;", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.GetDateValue = getDateValue;", currentClientId));

        
        sbScriptBlock.AppendLine("</script>");

        scriptBlockLiteral.Text = sbScriptBlock.ToString();

        if (!isSetDisplayMethodCalled)
        {
            SetDisplay();
        }

    }

    private void SetDisplay()
    {
        if (IsReadOnly)
        {
            if (Value != null)
            {
                lblDate.Text = Value.Value.ToString(displayFormat);
            }
            else
            {
                lblDate.Text = string.Empty;
            }
        }
    }

    private string displayFormat = DateUtil.DateFormat;
    public string DisplayFormat
    {
        get
        {
            return displayFormat;
        }
        set
        {
            displayFormat = value;
        }
    }

    public DateTime? OriginalValue
    {
        get
        {
            string strDate = hdnOriginal.Value; 
            DateTime? val = null;
            if (strDate != string.Empty)
            {
                try
                {
                    val = DateUtil.ToDate(strDate);
                }
                catch (Exception ex)
                {
                    Logger.Debug("String conversion to 'Date' failed", ex);
                }
            }
            return val;
        }
    }
    public bool HasValueChanged
    {
        get
        {
            return (Value != OriginalValue);
        }
    }
    public DateTime? Value
    {
        get
        {
            string strDate = (isReadOnly && string.IsNullOrEmpty(txtDatePicker.Text)) ? hdnCurrent.Value : txtDatePicker.Text.Trim();
            DateTime? val = null;
            if (strDate != string.Empty)
            {
                try
                {
                    if (ViewState["DateTime"] != null)
                    {
                        DateTime dt = (DateTime)ViewState["DateTime"];
                        val = DateUtil.ToDateTime(strDate + " " + dt.ToString("HH:mm"));
                    }
                    else
                    {
                        val = DateUtil.ToDate(strDate);
                    }
                }
                catch(Exception ex)
                
                {
                    Logger.Debug("String conversion to 'Date' failed",ex);
                }
            }
            return val;
        }
        set
        {
            txtDatePicker.Text =
                hdnCurrent.Value =
                hdnOriginal.Value = string.Empty;
            ViewState["DateTime"] = value;
            
            if (value != null)
            {
                try
                {
                    if (ShowingTime)
                    {
                        txtDatePicker.Text=DateUtil.DateTimeToString(value.Value);
                    }
                    else
                    {
                        txtDatePicker.Text = DateUtil.ToString(value.Value);
                    }

                    hdnCurrent.Value =
                        hdnOriginal.Value = txtDatePicker.Text;

                }
                catch(Exception ex)
                {
                    Logger.Debug("Date conversion to 'String' failed", ex);
                }
            }


            SetDisplay();
            isSetDisplayMethodCalled = true;


        }
    }


    private bool showingTime = false;
    public bool ShowingTime
    {
        get { return showingTime; }
        set { showingTime = value; }
    }

    public int MaxLength
    {
        get
        {
            return txtDatePicker.MaxLength;
        }
        set
        {
            txtDatePicker.MaxLength = value;
        }
    }
    public TextBoxMode TextMode
    {
        get
        {
            return txtDatePicker.TextMode;
        }
        set
        {
           
            txtDatePicker.TextMode = value;
        }
    }
    public string ValidationGroup
    {
        get
        {
            return txtDatePicker.ValidationGroup;
        }
        set
        {
            txtDatePicker.ValidationGroup =
                vldrRequired .ValidationGroup =
                vldrValidateDate.ValidationGroup = value;
        }
    }



    #region IASCXControl Members

     public string DisplayCssClass
    {
        get
        {
            return lblDate.CssClass;
        }
        set
        {
            lblDate.CssClass = value;
        }
    }
    object IASCXControl.OriginalValue
    {
        get
        {
            return OriginalValue;
        }
       
    }

    object IASCXControl.Value
    {
        get
        {
            return Value;
        }
        set
        {
            Value = value as DateTime?;
        }
    }

    private bool isReadOnly = false;
    public bool IsReadOnly
    {
        get
        {
            return isReadOnly;
        }
        set
        {
            isReadOnly = value;
            if (isReadOnly)
            {
                multiView.SetActiveView(viewReadOnly);
            }
            else
            {
                multiView.SetActiveView(viewEditable);
            }
        }
    }


    public string Text
    {
        get
        {
             
            return (IsReadOnly)? hdnCurrent.Value:
                txtDatePicker.Text;
        }
    }

    public string InputClientId
    {
        get
        {
            return txtDatePicker.ClientID;
        }
    }
    public string InputUniqueId
    {
        get
        {
            return txtDatePicker.UniqueID;
        }
    }

    public string OriginalClientId
    {
        get
        {
            return hdnOriginal.ClientID;
        }
    }
    public string CurrentClientId
    {
        get
        {
            return hdnCurrent.ClientID;
        }
    }

    public string DisplayClientId
    {
        get
        {
            return lblDate.ClientID;
        }
    }
    public string DisplayUniqueId
    {
        get
        {
            return lblDate.UniqueID;
        }
    }

    public Control InputControl
    {
        get
        {
            return txtDatePicker;
        }
    }

    public string JSResetFunction
    {
        get
        {
            return "ResetTextBox";
        }
    }

    private bool isRequired = false;
    public bool IsRequired
    {
        get
        {
            return isRequired;
        }
        set
        {
            isRequired = value;

            if (isRequired)
            {
                txtDatePicker.CssClass = "requiredField";
                vldrRequired.Enabled = true;
                vldrRequired.Display = ValidatorDisplay.Dynamic;
            }
            else
            {
                txtDatePicker.CssClass = "";
                vldrRequired.Enabled = false;
                vldrRequired.Display = ValidatorDisplay.None;
            }
        }
    }

    private string caption = string.Empty;
    public string Caption
    {
        get
        {
            return caption;
        }
        set
        {
            caption = value;
        }
    }


    #endregion


   
}
